package com.stylefeng.guns.modular.system.task;

import java.util.Hashtable;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.common.constant.state.KpMethod;
import com.stylefeng.guns.core.util.HttpsClient;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.ParamsMapper;
import com.stylefeng.guns.modular.system.model.Gpdata;
import com.stylefeng.guns.modular.system.model.Params;
import com.stylefeng.guns.modular.system.service.IGpdataService;

@Service
public class GpConceptImportTask {
	
	@Autowired
	private IGpdataService iGpdataService;
	
	@Autowired
	private ParamsMapper paramsMapper;
	
	public void execute(boolean isHoliday) {
		if(isHoliday){
			Params params = new Params();
			params.setgUserid("387065");
			params = paramsMapper.selectOne(params);
			if(params == null){
				return;
			}
			Hashtable<String, String> paramsMaps = new Hashtable<String, String>();
			paramsMaps.put("a", KpMethod.F.getMethod());
			paramsMaps.put("c", "PCArrangeData");
			paramsMaps.put("PIndex", "0");
			paramsMaps.put("POrder", "0");
			paramsMaps.put("Pst", "300");
			paramsMaps.put("PStart", "");
			paramsMaps.put("PType", "2");
			paramsMaps.put("SelType", "2");
			paramsMaps.put("ZSType", "5");
			paramsMaps.put("Token", params.getgToken());
			paramsMaps.put("UserID", params.getgUserid());
			String result = HttpsClient.postSSL(ImportContants.getKpDataPathStr(), paramsMaps,true);
			if(StringUtils.isNotEmpty(result)){
				JSONObject jsonObj = JSON.parseObject(result).getJSONObject("plates");
				if(jsonObj != null){
					JSONArray jsonArray = JSON.parseArray(jsonObj.getString("list"));
					for(int i=0;i<jsonArray.size();i++){
						JSONArray entity = (JSONArray)jsonArray.get(i);
						paramsMaps = new Hashtable<String, String>();
						paramsMaps.put("a", KpMethod.F.getMethod());
						paramsMaps.put("c", "PCArrangeData");
						paramsMaps.put("LEnd", "");
						paramsMaps.put("LIndex", "0");
						paramsMaps.put("LOrder", "1");
						paramsMaps.put("Lst", "300");
						paramsMaps.put("LStart", "");
						paramsMaps.put("LType", "6");
						paramsMaps.put("SelType", "3");
						paramsMaps.put("PlateID", entity.getString(0));
						paramsMaps.put("Token", params.getgToken());
						paramsMaps.put("UserID", params.getgUserid());
						String resultc = HttpsClient.postSSL(ImportContants.getKpDataPathStr(), paramsMaps,true);
						if(StringUtils.isNotEmpty(resultc)){
							JSONObject jsonObjc = JSON.parseObject(resultc).getJSONObject("stocks");
							if(jsonObjc != null){
								JSONArray jsonArrayc = JSON.parseArray(jsonObjc.getString("list"));
								for(int j=0;j<jsonArrayc.size();j++){
									JSONArray entityc = (JSONArray)jsonArrayc.get(j);
									 Wrapper<Gpdata> wrapper = new EntityWrapper<Gpdata>();
									 wrapper.eq("g_dm", entityc.get(0));
									 Gpdata gpdata = iGpdataService.selectOne(wrapper);
									 if(gpdata != null){
										 gpdata.setgConceptBk(entityc.getString(12));
										 iGpdataService.updateById(gpdata);
									 }
								}
							}
						}
					}
				}
			}
		}
	}
}
